﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YuFeng.Models.Banner;
using YuFeng.Services.Banner;
using YuFeng.Web.Extentions;
using YuFeng.Web.Filters;

namespace YuFeng.Web.Areas.Admin.Controllers
{
    [UserLoginFilter]
    public class BannerController : BaseController
    {
        private readonly IBannerService _bannerService;
        public BannerController(IBannerService BannerService)
        {
            _bannerService = BannerService;
        }
        public ActionResult Index(int page = 1)
        {
            var Banner = _bannerService.GetPageBanners(false, page, 10);
            return View(Banner);
        }
        public ActionResult Create()
        {
            Banner banner = new Banner();
            return View(banner);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Banner banner)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _bannerService.Create(banner);
                    return this.ProcessFormatedResult(true, "创建成功！", "Index");
                }
                else
                {
                    return this.ProcessFormatedResult(false, this.ExtendErrors(), model: banner);
                }
            }
            catch (Exception e)
            {
                return this.ProcessFormatedResult(false, e.Message, model: banner);
            }
        }
        public ActionResult Edit(string id)
        {
            try
            {
                var banner = _bannerService.GetBannerById(id);
                if (banner == null)
                {
                    throw new Exception(string.Format("编号为{0}的轮播图不存在", id));
                }
                return View(banner);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Banner banner)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _bannerService.Update(banner);
                    return this.ProcessFormatedResult(true, "编辑成功！", "Edit", null, new { id = banner.Id });
                }
                else
                {
                    return this.ProcessFormatedResult(false, this.ExtendErrors(), model: banner);
                }
            }
            catch (Exception e)
            {
                return this.ProcessFormatedResult(false, e.Message, model: banner);
            }
        }
        [HttpPost]
        public ActionResult DeleteBanner(string id)
        {
            try
            {
                var banner = _bannerService.GetBannerById(id);
                if (banner == null)
                {
                    return ErrorMsg("轮播图不存在或已删除！");
                }
                else
                {
                    banner.IsDeleted = true;
                    _bannerService.Update(banner);
                    return SuccessMsg("删除成功！");
                }
            }
            catch (Exception e)
            {
                return ErrorMsg(e.Message);
            }
        }
    }
}